﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1._Controls
{
	public partial class DicCascadingDropDownListDataGenerator : System.Web.UI.Page
	{
		private LoadedValue<int?> loadedSiteId;
		public int? SiteId
		{
			get
			{
				if (loadedSiteId.Loader == null)
					loadedSiteId.Loader = () => IntExtensions.ParseOrNull(Request.QueryString["SiteId"]);
				return loadedSiteId.Value;
			}
		}

		private LoadedValue<string> loadedClassCode;
		public string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => Request.QueryString["ClassCode"];
				return loadedClassCode.Value;
			}
		}

		private LoadedValue<int?> loadedClassId;
		protected int? ClassId
		{
			get
			{
				if (loadedClassId.Loader == null)
					loadedClassId.Loader = () => { return DicClasses.GetIdentifier(SiteId, ClassCode); };
				return loadedClassId.Value;
			}
		}

		private LoadedValue<DicValueType> loadedValueType;
		public DicValueType ValueType
		{
			get
			{
				if (loadedValueType.Loader == null)
					loadedValueType.Loader = () => EnumExtensions.ParseOrDefault<DicValueType>(Request.QueryString["ValueType"], DicValueType.DicId);
				return loadedValueType.Value;
			}
		}

		private LoadedValue<DicTextType> loadedTextType;
		public DicTextType TextType
		{
			get
			{
				if (loadedTextType.Loader == null)
					loadedTextType.Loader = () => EnumExtensions.ParseOrDefault<DicTextType>(Request.QueryString["TextType"], DicTextType.Name);
				return loadedTextType.Value;
			}
		}

		private LoadedValue<string> loadedParentValue;
		public string ParentValue
		{
			get
			{
				if (loadedParentValue.Loader == null)
					loadedParentValue.Loader = () => Request.QueryString["ParentValue"];
				return loadedParentValue.Value;
			}
		}

		protected override void Render(HtmlTextWriter writer)
		{
			List<Dictionary> dics;
			if (ClassId.HasValue)
				dics = ValueType == DicValueType.Value ? Dictionaries.GetItemsByParentValue(ClassId.Value, ParentValue) : Dictionaries.GetItems(ClassId.Value, IntExtensions.ParseOrNull(ParentValue));
			else
				dics = new List<Dictionary>();

			StringBuilder sb = new StringBuilder();

			for (int i = 0; i < dics.Count; i++)
			{
				Dictionary dic = dics[i];
				sb.Append((i == 0 ? "" : ",") + @"{text:""" + (TextType == DicTextType.Value ? dic.Value : dic.Name) + @""", value:""" + (ValueType == DicValueType.Value ? dic.Value : dic.DicId.ToString()) + @""", childCount:" + dic.ChildCount + "}");
			}

			Response.ClearContent();
			Response.ContentType = "text/javascript";
			Response.Write(Request.QueryString["callback"]);
			Response.Write("([");
			Response.Write(sb.ToString());
			Response.Write("])");

			//base.Render(writer);
		}
	}
}
